#ifndef DIGITALFILTERS_H_
#define DIGITALFILTERS_H_

/************************************************************************************
*	Copyright Paxs Electronics 2012													*
*   All Rights Reserved																*
*   The Copyright symbol does not also indicate public availability or publication.	*
* 																					*
* 								PAXS ELECTRONICS									*
* 																					*
* - Driver:   			Digital Filter Driver										*
* 																					*
* - Compiler:           Code Composer Studio (Licensed)								*
* - Supported devices:  All Stellaris Family Cortex-M3.								*
* - AppNote:																		*
*																					*
* 	Created by: 		Juan Francisco Gomez Ruiz									*
*   Date Created: 		14 - MAR - 2012												*
*   Contac				pacoduz@gmail.com											*
* 																					*
* 	Description: 		Digital Filters Driver, FIR or IIR filter type selection	*
* 						Number of channels, Order selection, N-Tap selection. 		*
* 						*All this options aply to all channels.						*
*   Device supported and tested: - LM3S5749 - LM3S3748 -							*
* 																				2012*
*************************************************************************************

*************************************************************************************
* 	Revision History:
*
*   Date          CP#           Author
*   MM-DD-YY      XXXXX:1       Initials   Description of change
*   -----------   -----------   --------   ------------------------------------
*    03-14-12      	1.0             JFGR	Initial version. 
*
*************************************************************************************
*  File/
*  	
* 
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,  
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
*  DEALINGS IN THE SOFTWARE.
* 
*/
	//FIR Filter at 8 Taps, takes 74us
	//IIR Filter 2 order,   takes 50us
	#include "lm3s5749.h"
	#define   GLOBAL_Q	16
	#include "IQmath/IQmathLib.h"
	#include "Imu9DOFDriver.h"
	#include "inc/hw_types.h"

/******************************************************************************
*   						MACRO DEFINITIONS
******************************************************************************/	
	//////////////////////////////////////////
	//		DIGITAL FILTER SELECTION		//
	//////////////////////////////////////////
	#define	FIR					0
	#define IIR					1
	#define FILTER_TYPE			(IIR)
	
	//////////////////////////////////////////
	//		DIGITAL FILTER ORDER			//
	//////////////////////////////////////////
	#define FILTER_ORDER		2
	//*Note: IF Digital filter is FIR, next define is important
	#define TAP_FILTER			8			//TAPS available = 8 or 16
	
	//////////////////////////////////////////
	//		ALL CHANNELS FREQ. CUT			//
	//////////////////////////////////////////
	#define FREQ_10HZ			0
	#define FREQ_20HZ			1
	#define FREQ_50HZ			2
	#define FREQ_100HZ			3
	#define FREQ_120HZ			4
	#define FREQ_150HZ			5
	#define FREQUENCY_CUT		(FREQ_150HZ)
	
	//////////////////////////////////////////
	//		DIGITAL FILTER CHANNELS			//
	//////////////////////////////////////////
	// 6 channels for all axes of Accelerometer and Gyroscope sensors
	#define FILTER_CHANNELS		6
	
/*-----------------------------------------------------------------------------*/

/******************************************************************************
*   						EXTERNAL VARIABLES
******************************************************************************/
		// FilterInputs - variable were new data is introduce it to the Filter
	extern _iq FilterInputs[FILTER_CHANNELS];		
		// FilterOutputs - variable were data filtered is store.
	extern _iq FilterOutputs[FILTER_CHANNELS];
	
	
/******************************************************************************
*   					GLOBAL FUNCTION PROTOTYPES
******************************************************************************/
	#if FILTER_TYPE == FIR
		void VfnFIRFilterSetup(void);
		void VfnFIRFilter (void);
	#endif
	
	#if FILTER_TYPE == IIR
		void VfnIIRFilterSetup(void);
		void VfnIIRFilter (void);
	#endif
	
/*	-------------------------------------------------------------------------------------------- */
/*	-------------------------------------------------------------------------------------------- */
	//////////////////////////////////////////////////////////
	//			DIGITAL FILTER COEFICIENTS SECTION			//
	//	*Note: User must define correct Filter coeficients 	//
	//////////////////////////////////////////////////////////
	
	  /* Filter type: Low Pass
		Filter model: Butterworth
		Filter order: 4
		Frequncy Cut: 10 Hz
		Sampling Frequency: 1100 Hz
		Cut Frequency: 10.000000 Hz
	*/
	#if FREQUENCY_CUT == FREQ_10HZ
    static _iq FIRCoefficients[TAP_FILTER] = { 
        _IQ(0.12430274997139619000),
        _IQ(0.12527998598460521000),
        _IQ(0.12588337566477809000),
        _IQ(0.12608831456892683000),
        _IQ(0.12588337566477809000),
        _IQ(0.12527998598460521000),
        _IQ(0.12430274997139619000),
        _IQ(0.12297946218951421000)
    };
    #endif
    
    /* Filter type: Low Pass
		Filter model: Butterworth
		Filter order: 4
		Frequncy Cut: 50 Hz
		Sampling Frequency: 1100 Hz
		Cut Frequency: 50.000000 Hz
	*/
	#if FREQUENCY_CUT == FREQ_50HZ
    static _iq FIRCoefficients[TAP_FILTER] = {
        _IQ(0.11094920841138470000),
        _IQ(0.12893767299049697000),
        _IQ(0.14169409456343993000),
        _IQ(0.14638319395852070000),
        _IQ(0.14169409456343993000),
        _IQ(0.12893767299049697000),
        _IQ(0.11094920841138470000),
        _IQ(0.09045485411083617500)
    };
    #endif
	/* Filter type: Low Pass
		Filter model: Butterworth
		Filter order: 4
		Frequncy Cut: 100 Hz
		Sampling Frequency: 1100 Hz
		Cut Frequency: 100.000000 Hz
	*/
	#if FREQUENCY_CUT == FREQ_100HZ
	static _iq FIRCoefficients[TAP_FILTER] = { 
        _IQ(0.07308563976120928400),
        _IQ(0.13067674419877340000),
        _IQ(0.18182878646751641000),
        _IQ(0.20293010263865291000),
        _IQ(0.18182878646751641000),
        _IQ(0.13067674419877340000),
        _IQ(0.07308563976120928400),
        _IQ(0.02588755650634900300)
    };
    #endif
    
    /*Filter type: Low Pass
	Filter model: Butterworth
	Filter order: 2
	Sampling Frequency: 1000 Hz
	Cut Frequency: 10.000000 Hz
	*/
	#if FREQUENCY_CUT == FREQ_10HZ
    static _iq ACoefficients[FILTER_ORDER+1] = {
        _IQ(0.00094547653094439164),
        _IQ(0.00189095306188878330),
        _IQ(0.00094547653094439164)
    };

    static _iq BCoefficients[FILTER_ORDER+1] = {
        _IQ(1.00000000000000000000),
        _IQ(-1.91119706742607360000),
        _IQ(0.91497583480143418000)
    };
    #endif
    /*Filter type: Low Pass
	Filter model: Butterworth
	Filter order: 2
	Frequncy Cut: 20 Hz
	Sampling Frequency: 1000 Hz
	Cut Frequency: 20.000000 Hz */
	#if FREQUENCY_CUT == FREQ_20HZ
  	static _iq ACoefficients[FILTER_ORDER+1] = {
        _IQ(0.00362453228686862190),
        _IQ(0.00724906457373724390),
        _IQ(0.00362453228686862190)
    };
    static _iq BCoefficients[FILTER_ORDER+1] = {
        _IQ(1.00000000000000000000),
        _IQ(-1.8226949251963080000),
        _IQ(0.83718165125602251000)
    };
    #endif
    
    /*Filter type: Low Pass
	Filter model: Chebyshev
	Filter order: 4
	Sampling Frequency: 1100 Hz
	Cut Frequency: 50.000000 Hz
	Pass band Ripple: 0.500000 dB
	*/
	#if FREQUENCY_CUT == FREQ_50HZ
    static _iq ACoefficients[FILTER_ORDER+1] = {
        _IQ(0.01692239500389296900),
        _IQ(0.03384479000778593800),
        _IQ(0.01692239500389296900)
    };

    static _iq BCoefficients[FILTER_ORDER+1] = {
        _IQ(1.00000000000000000000),
        _IQ(-1.60020175277239570000),
        _IQ(0.66775765615488569000)
    };
    #endif
    
    /* Filter type: Low Pass
		Filter model: Chebyshev
		Filter order: 2
		Frequncy Cut: 100 Hz
		Sampling Frequency: 1000 Hz
		Cut Frequency: 100.000000 Hz
		Pass band Ripple: 0.500000 dB
	*/
	#if FREQUENCY_CUT == FREQ_100HZ
	static _iq ACoefficients[FILTER_ORDER+1] = {
        _IQ(0.09860870794546082500),
        _IQ(0.19721741589092165000),
        _IQ(0.09860870794546082500)
    };

    static _iq BCoefficients[FILTER_ORDER+1] = {
        _IQ(1.00000000000000000000),
        _IQ(-1.03485356293647610000),
        _IQ(0.42928816310990331000)
    };
    #endif
    
    /*Filter type: Low Pass
	Filter model: Chebyshev
	Filter order: 2
	Sampling Frequency: 1100 Hz
	Cut Frequency: 120.000000 Hz
	Pass band Ripple: 0.500000 dB	*/
    #if FREQUENCY_CUT == FREQ_120HZ
    static _iq ACoefficients[FILTER_ORDER+1] = {
        _IQ(0.11342955061058495000),
        _IQ(0.22685910122116990000),
        _IQ(0.11342955061058495000)
    };

    static _iq BCoefficients[FILTER_ORDER+1] = {
        _IQ(1.00000000000000000000),
        _IQ(-0.94844772079215278000),
        _IQ(0.40216623323550099000)
    };
    #endif
    
    /*Filter type: Low Pass
	Filter model: Chebyshev
	Filter order: 2
	Sampling Frequency: 1100 Hz
	Cut Frequency: 150.000000 Hz
	Pass band Ripple: 0.500000 dB */
    #if FREQUENCY_CUT == FREQ_150HZ
    static _iq ACoefficients[FILTER_ORDER+1] = {
        _IQ(0.16073987910438187000),
        _IQ(0.32147975820876373000),
        _IQ(0.16073987910438187000)
    };

   static _iq BCoefficients[FILTER_ORDER+1] = {
        _IQ(1.00000000000000000000),
        _IQ(-0.69515155167579734000),
        _IQ(0.33811109524003596000)
    };
    #endif

#endif /*DIGITALFILTERS_H_*/
